#ifndef _SI446X_H_
#define _SI446X_H_

#include <stdint.h>
#include "SI446x_defs.h"

#define SI_CSN_LOW      HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET)
#define SI_CSN_HIGH     HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET)

#define SI_SDN_LOW      HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, GPIO_PIN_RESET)
#define SI_SDN_HIGH     HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, GPIO_PIN_SET)

#define Get_GPIO0()     (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_3) == GPIO_PIN_SET)
#define Get_IRQ()       (HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_1) == GPIO_PIN_SET)

#define  PACKET_LENGTH      8 //0-64, if = 0: variable mode, else: fixed mode

/*Send a command*/
uint8_t SI446x_CMD( uint8_t *cmd, uint8_t cmdsize );

/*wait the device ready to response a command*/
uint8_t SI446x_Wait_CTS( void );

/*wait the device ready to response a command*/
uint8_t SI446x_PollCTS( void );

/*send a nop command*/
uint8_t SI446X_NOP( void );

/*read a array of command response*/
void SI446x_Read_Response( uint8_t *buffer, uint8_t size );

/*Read the PART_INFO, 8 bytes needed*/
void SI446x_PART_Info( uint8_t *buffer );

/*Read the FUNC_INFO, 7 bytes needed*/
void SI446x_FUNC_Info( uint8_t *buffer );

/*read packet information*/
void SI446x_PKT_Info( uint8_t *buffer, uint8_t FIELD, uint16_t length, uint16_t diff_len );

/*read fifo information*/
void SI446x_FIFO_Info( uint8_t *buffer );

/*Read the INT status, 9 bytes needed*/
void SI446x_INT_Status( uint8_t *pData );

/*Read the PH status of, 3 bytes needed*/
void SI446x_PH_Status( uint8_t *pData );

/*reset the SI446x device*/
void SI446x_Reset( void );

/*Power up the device*/
void SI446x_Power_Up( uint32_t XO_FREQ );

/*configuration the device*/
void SI446x_Config_Init( void );

/*config the CRC, PROPERTY 0x1200*/
void SI446x_CRC_Config( uint8_t PKT_CRC_CONFIG );

/*config the GPIOs, IRQ, SDO*/
void SI446x_GPIO_Config( uint8_t G0, uint8_t G1, uint8_t G2, uint8_t G3,
                         uint8_t IRQ, uint8_t SDO, uint8_t GEN_CONFIG );

/*set the tx power*/
void SI446x_Set_Power( uint8_t Power_Level );

/*reset the RX FIFO*/
void SI446x_RX_FIFO_Reset( void );

/*reset the TX FIFO*/
void SI446x_TX_FIFO_Reset( void );

/*write data to TX fifo*/
void SI446x_Write_TX_FIFO( uint8_t *txbuffer, uint8_t size );

/*start TX command*/
void SI446x_Start_TX( uint8_t channel, uint8_t condition, uint16_t tx_len );

/*send a packet*/
void SI446x_Send_Packet( uint8_t *txbuffer, uint8_t size, uint8_t channel, uint8_t condition );

/*start RX state*/
void SI446x_Start_RX( uint8_t channel, uint8_t condition, uint16_t rx_len,
                      uint8_t n_state1, uint8_t n_state2, uint8_t n_state3 );

/*read RX fifo*/
uint8_t SI446x_Read_Packet( uint8_t *buffer );

/*Issue a change state command to the radio. */
void SI446x_Change_State( uint8_t NewState );

/*Requests the current state of the device  */
uint8_t SI446x_Get_Device_State( void );

/*Set the PROPERTY of the device, only 1 byte*/
void SI446x_Set_Property_1( SI446X_PROPERTY GROUP_NUM, uint8_t proirity );

/*Get the PROPERTY of the device, only 1 byte*/
uint8_t SI446x_Get_Property_1( SI446X_PROPERTY GROUP_NUM );

/*Set the PROPERTY*/
void SI446x_Set_Property_n( SI446X_PROPERTY GROUP_NUM, uint8_t NUM_PROPS, uint8_t *PAR_BUFF );

/*Read the PROPERTY*/
void SI446x_Get_Property_n( SI446X_PROPERTY GROUP_NUM, uint8_t NUM_PROPS, uint8_t *buffer  );

void Si446x_FRR_Read(uint8_t *pData);
/*
=================================================================================
----------------------------PROPERTY fast setting macros-------------------------
=================================================================================
*/
// GOLBAL(0x00)
#define GLOBAL_XO_TUNE( x )                 SI446x_Set_Property_1( 0x0000, x )
#define GLOBAL_CLK_CFG( x )                 SI446x_Set_Property_1( 0x0001, x )
#define GLOBAL_LOW_BATT_THRESH( x )         SI446x_Set_Property_1( 0x0002, x )
#define GLOBAL_CONFIG( x )                  SI446x_Set_Property_1( 0x0003, x )
#define GLOBAL_WUT_CONFIG( x )              SI446x_Set_Property_1( 0x0004, x )
#define GLOBAL_WUT_M_15_8( x )              SI446x_Set_Property_1( 0x0005, x )
#define GLOBAL_WUT_M_7_0( x )               SI446x_Set_Property_1( 0x0006, x )
#define GLOBAL_WUT_R( x )                   SI446x_Set_Property_1( 0x0007, x )
#define GLOBAL_WUT_LDC( x )                 SI446x_Set_Property_1( 0x0008, x )
#define GLOBAL_WUT_CAL( x )                 SI446x_Set_Property_1( 0x0009, x )

// INT_CTL(0x01)
#define INT_CTL_ENABLE( x )                 SI446x_Set_Property_1( 0x0100, x )
#define INT_CTL_PH_ENABLE( x )              SI446x_Set_Property_1( 0x0101, x )
#define INT_CTL_MODEM_ENABLE( x )           SI446x_Set_Property_1( 0x0102, x )
#define INT_CTL_CHIP_ENABLE( x )            SI446x_Set_Property_1( 0x0103, x )

//group 0x02, FRR_CTL
#define FRR_CTL_A_MODE( x )                 SI446x_Set_Property_1( 0x0200, x )
#define FRR_CTL_B_MODE( x )                 SI446x_Set_Property_1( 0x0201, x )
#define FRR_CTL_C_MODE( x )                 SI446x_Set_Property_1( 0x0202, x )
#define FRR_CTL_D_MODE( x )                 SI446x_Set_Property_1( 0x0203, x )

// PREAMBLE (0x10)

#endif //_SI446X_H_

/*
=================================================================================
------------------------------------End of FILE----------------------------------
=================================================================================
*/
